package middleware

import (
	"fmt"
	redis_modules "hft/db/redis/modules"
	"hft/global"
	"net"

	"github.com/gin-gonic/gin"
)

// 判断ip所属

func GetIpRegion() gin.HandlerFunc {
	return func(c *gin.Context) {
		realIP := c.Request.Header.Get("h-real-ip")
		// fmt.Println(redis_modules.IpCacheDB.IsInBlackIP(realIP), "asda", realIP, "referer:", c.Request.Header.Get("Referer"), "user-agent:", c.Request.UserAgent(), redis_modules.IpCacheDB.IsInBlackIP(realIP), redis_modules.IpCacheDB.IsInBlackIP(realIP) || (c.Request.Header.Get("Referer") == "" && realIP != ""))
		if redis_modules.IpCacheDB.IsInBlackIP(realIP) {
			c.Redirect(403, "/city/404")
			c.Abort()
			return
		} else {
			ip := net.ParseIP(realIP)
			record, _ := global.OC_GEO.City(ip)
			fmt.Println("当前地区", record.Country.IsoCode, ip)
			region := record.Country.IsoCode
			if region == "" || region == "PRIVATE" || region == "CN" {
				c.Header("lang", "zh-CN")
			} else {
				c.Header("lang", "zh-HK")
			}
			c.Next()
		}
	}

}
